<?php
require_once(dirname(__FILE__).'/../config/db_connect.php');
require_once(dirname(__FILE__).'/../service/service.cl.php');

class Employe {

    private $error = array();
    private $matricule;
    private $last_name;
    private $first_name;
    private $type; 
    private $adresse; 
    private $tele_bureau; 
    private $gsm; 
    private $serviceId;
    private $id;
    private $role;
    private $password;

    private $service;
    const ENGINEER = 'Ingenieur';
    const AGENT = 'Agent';

    public function __construct() {

        $this->service = new Service();
    }

    public function setValues($data) {

       $this->id =  empty($data['id']) ? '' : strip_tags(trim($data['id']));
       $this->matricule = empty($data['matricule']) ? '' : strip_tags(trim($data['matricule']));
       $this->role = empty($data['role']) ? '' : strip_tags(trim($data['role']));
       $this->password = empty($data['password']) ? '' : strip_tags(trim($data['password']));
       $this->last_name =  empty($data['last_name']) ? '' : strip_tags(trim($data['last_name']));
       $this->first_name = empty($data['first_name']) ? '' : strip_tags(trim($data['first_name']));
       $this->type = empty($data['type']) ? '' : strip_tags(trim($data['type']));
       $this->adresse=  empty($data['adresse']) ? '' : strip_tags(trim($data['adresse']));
       $this->tele_bureau = empty($data['tele_bureau']) ? '' : strip_tags(trim($data['tele_bureau']));
       $this->gsm =  empty($data['gsm']) ? '' : strip_tags(trim($data['gsm']));
       $this->serviceId = empty($data['service_id']) ? '' : strip_tags(trim($data['service_id']));
       $this->getService()->loadServiceByID($this->getServiceId());
    }
    public function reset() {

       $this->id = '';
       $this->matricule = '';
       $this->last_name =  '';
       $this->first_name = '';
       $this->type = '';
       $this->password = '';
       $this->adresse=  '';
       $this->tele_bureau = '';
       $this->gsm = '';
       $this->serviceId ='';
       $this->getService()->reset();
    }

    public function save($data) {

      $this->setValues($data);
        if($this->getId()) {
            if($this->validate()) {
                $password = '';
                if(!empty(getSession()->id) && (getSession()->id == $this->getId())) {
                    $password = ", password='".md5($this->getPassword())."'";
                }
                $update="UPDATE employe SET matricule='$this->matricule' , last_name='$this->last_name' , first_name='$this->first_name', type='$this->type' , adresse='$this->adresse' , tele_bureau='$this->tele_bureau', gsm='$this->gsm' , service_id='$this->serviceId' $password WHERE id='$this->id' ";
                mysql_query ($update);
            }
        } else {
            if($this->validate()) {
                mysql_query ("INSERT INTO employe VALUES ('','".$this->matricule."','".$this->last_name. "','".$this->first_name."','','".$this->type."','".$this->adresse."','".$this->tele_bureau."','".$this->gsm."','".$this->serviceId."','','')");
                $this->reset();
            }  
  
         }
    }

    public function saveOrder($orders) {
 
        if(is_array($orders)) {
            foreach($orders as $key=>$value) {
                $sql = "UPDATE employe SET `order`='$value' WHERE id = '$key'";
                mysql_query($sql);
            }
        }
    }

    public function loadEmployeById($id) {
        
        $select = "SELECT * FROM employe WHERE id = '$id' ";
        $resource = mysql_query($select);
        $this->setValues(mysql_fetch_array($resource));
    }

    public function loadEmployeByMatricule($matricule) {
        
        $select = "SELECT * FROM employe WHERE  matricule = '$matricule' /*ORDER BY type DESC*/";
        $resource = mysql_query($select);
        $this->setValues(mysql_fetch_array($resource));
    }
     public function getEmployesByServiceId($serviceId) {
        
        $select = "SELECT * FROM employe " ;
        $select .= $serviceId == 0 ? '' : "WHERE service_id='$serviceId' ORDER BY type DESC ";
        $resource = mysql_query($select);
 
        $employes = array();
        while($employe = mysql_fetch_object($resource)) {
              $employes[] = $employe;
        } 
        return $employes; 
    }

     public function getEmployesByServiceIdAndType($serviceId=0, $type=0) {
        
        $select = "SELECT * FROM employe";
        $select .= ($serviceId !=0 || $type !='') ?  " WHERE " : "";
        $select .= $serviceId == 0 ? '' : " service_id='$serviceId'";
        $select .= ($serviceId !=0 && $type !='') ?  " AND " : "";
        $select .= $type == '' ? '' : " type ='$type'";
        $select .= " ORDER by `order`";

        $this->type = ucfirst($type);
        $resource = mysql_query($select);

        $employes = array();
        while($employe = mysql_fetch_object($resource)) {
              $employes[] = $employe;
        } 
        return $employes; 
    }


    private function validate() {

        /*if (empty($this->departementId)) {
           $this->addError("Selectionner un departement");
       }*/
       if (empty($this->serviceId)) {
           $this->addError("service_id","Selectionner un service");
       }
       if (empty($this->matricule)) {
           $this->addError("matricule", "Saisissez le matricule");
       }
       if (empty($this->last_name)) {
           $this->addError("last_name", "Saisissez le nom");
       }
       if (empty($this->first_name)) {
           $this->addError("first_name", "Saisissez le prenom");
       }
       if (empty($this->type)) {
           $this->addError("type","Selectionner un type");
       }
       if (empty($this->adresse)) {
           $this->addError("adresse","Saisissez l'adresse");
       }
       /* if (empty($this->tele_bureau)) {  
            $this->addError("tele_bureau", "Saisissez le telephone du bureau");
       }    */
       if (empty($this->gsm)) {
           $this->addError("gsm", "Saisissez le numéro gsm");
       }
       if(count($this->error) > 0) {
            return false;
        } else {
            return true;
        }     
    }

    public function login() {

        $select = "SELECT * FROM employe WHERE matricule='".mysql_real_escape_string($this->getMatricule())."' AND password='".md5($this->getPassword())."'";
        $resource = mysql_query($select);
        $data = mysql_fetch_object($resource);

        if(!empty($data)) {
            unset($data->password);
            session_start();
            $_SESSION['auth'] = $data;
            header('location: ../guard/list.php');
            die();
        } else {
            $this->addError('login', 'Login ou password incorrect');
        }

    }


    private function addError($index, $error) {
        $this->error[$index] =  $error;
    }

    public function getError($index) { 
      if(!empty($this->error[$index])) {
        return '<div class="alert alert-error">'.$this->error[$index].'</div>';
      }
    }
    public function hasError() {
       return count($this->error) > 0 ? true : false;
    }
    public function getMatricule(){
        return $this->matricule;
    }
    public function getLastName(){
        return $this->last_name;
    }
    public function getFirstName(){
        return $this->first_name;
    }

    public function getType(){
        return $this->type;
    }
    public function getAdresse(){
        return $this->adresse;
    }
    public function getTele_bureau(){
        return $this->tele_bureau;
    }
    public function getGSM(){
        return $this->gsm;
    }
    public function getServiceId(){
        return $this->serviceId;
    }
    public function getId(){
        return $this->id;
    }
    public function getPassword(){
        return $this->password;
    }
    public function getService(){
        return $this->service;
    }
}
